package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import  entity.Account;
import  service.inter.IAccountService;
import  service.inter.ICommonService;
import  tool.JsonUtil;

@Controller
@RequestMapping("account/")
public class AccountController {
	@Autowired
	private IAccountService accountService;
	@Autowired
	private ICommonService iCommonService;
	@RequestMapping("login.do")
	public void login(Account account,
			HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/javascript;charset=UTF-8");  
        response.setHeader("Pragma", "No-cache");  
        response.setHeader("Cache-Control", "no-cache");  
        response.setDateHeader("Expires", 0); 
        response.setHeader("Access-Control-Allow-Origin", "*");
		Map map=accountService.login(account, request, response);
		String json=JsonUtil.getJson(map);
		json=json.replaceAll("\\\\\\\"", "\"");
		try {
			String callback=(String)request.getAttribute("callback");
			 PrintWriter out = response.getWriter(); 
			 if(callback!=null){
				 out.print(callback+"("+json+")");
			 }else{
				 out.print(json);
			 }
			 out.flush();  
		     out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		return map;
	}
	@RequestMapping("register.do")
	public void register(Account account,
			HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/javascript;charset=UTF-8");  
        response.setHeader("Pragma", "No-cache");  
        response.setHeader("Cache-Control", "no-cache");  
        response.setDateHeader("Expires", 0); 
        response.setHeader("Access-Control-Allow-Origin", "*");
			Map map=accountService.register(account, request, response);
			String json=JsonUtil.getJson(map);
			json=json.replaceAll("\\\\\\\"", "\"");
			try {
				String callback=(String)request.getAttribute("callback");
				 PrintWriter out = response.getWriter(); 
				 if(callback!=null){
					 out.print(callback+"("+json+")");
				 }else{
					 out.print(json);
				 }
				 out.flush();  
			     out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
//			return map;
	}
	@RequestMapping(value="updateAccount.do")
	public void updateAccount(Account account,
			HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/javascript;charset=UTF-8");  
        response.setHeader("Pragma", "No-cache");  
        response.setHeader("Cache-Control", "no-cache");  
        response.setDateHeader("Expires", 0); 
        response.setHeader("Access-Control-Allow-Origin", "*");
//        response.setHeader("Access-Control-Allow-Methods", "*");
			Map map=accountService.updateAccount(account, request, response);
			String json=JsonUtil.getJson(map);
			json=json.replaceAll("\\\\\\\"", "\"");
			try {
				String callback=(String)request.getAttribute("callback");
				 PrintWriter out = response.getWriter(); 
				 if(callback!=null){
					 out.print(callback+"("+json+")");
				 }else{
					 out.print(json);
				 }
				 out.flush();  
			     out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
//			return map;
	}
	@RequestMapping("/visit")
	public void visit(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/javascript;charset=UTF-8");  
        response.setHeader("Pragma", "No-cache");  
        response.setHeader("Cache-Control", "no-cache");  
        response.setDateHeader("Expires", 0); 
        response.setHeader("Access-Control-Allow-Origin", "*");
//        response.setHeader("Access-Control-Allow-Methods", "*");
			Map map=iCommonService.confirm(request);
			String json=JsonUtil.getJson(map);
			json=json.replaceAll("\\\\\\\"", "\"");
			try {
				String callback=(String)request.getAttribute("callback");
				 PrintWriter out = response.getWriter(); 
				 if(callback!=null){
					 out.print(callback+"("+json+")");
				 }else{
					 out.print(json);
				 }
				 out.flush();  
			     out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
//			return map;
	}
	@RequestMapping("/getList")
	public void getList(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/javascript;charset=UTF-8");  
        response.setHeader("Pragma", "No-cache");  
        response.setHeader("Cache-Control", "no-cache");  
        response.setDateHeader("Expires", 0); 
        response.setHeader("Access-Control-Allow-Origin", "*");
//        response.setHeader("Access-Control-Allow-Methods", "*");
			Map map=accountService.getList(request);
			String json=JsonUtil.getJson(map);
			json=json.replaceAll("\\\\\\\"", "\"");
			try {
				String callback=(String)request.getAttribute("callback");
				 PrintWriter out = response.getWriter(); 
				 if(callback!=null){
					 out.print(callback+"("+json+")");
				 }else{
					 out.print(json);
				 }
				 out.flush();  
			     out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
//			return map;
	}
}
